<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <title>layui</title>
    <meta name="renderer" content="webkit" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
    <meta
      name="viewport"
      content="width=device-width, initial-scale=1, maximum-scale=1"
    />
    <link
      rel="stylesheet"
      href="../../lib/layui-v2.5.5/css/layui.css"
      media="all"
    />
    <link rel="stylesheet" href="../../css/public.css" media="all" />
  </head>
  <style>
    /* td .layui-form-select {
	     margin-top: -10px;
	     margin-left: -15px;
	     margin-right: -15px;
	 } */

    .layui-form-item .layui-inline {
      margin-right: -10px;
    }

    /* .layui-input{
        height: 100%;
     } */
  </style>
  <body>
    <div class="layuimini-container">
      <div class="layuimini-main">
        <div class="layui-form layuimini-form">
          <div class="layui-form-item">
            <div class="layui-inline">
              <label class="layui-form-label">试验结论</label>
              <div class="layui-input-inline">
                <input
                  type="text"
                  name="report"
                  placeholder="请填写实验结论"
                  autocomplete="off"
                  class="layui-input"
                />
              </div>
            </div>
            <div class="layui-inline">
              <label class="layui-form-label">试验人员</label>
              <div class="layui-input-inline">
                <input
                  type="text"
                  name="experimenter"
                  placeholder="请填写试验人员"
                  autocomplete="off"
                  class="layui-input"
                />
              </div>
            </div>
            <div class="layui-inline">
              <label class="layui-form-label">审核人员</label>
              <div class="layui-input-inline">
                <input
                  type="text"
                  name="auditors"
                  placeholder="请填写审核人员"
                  autocomplete="off"
                  class="layui-input"
                />
              </div>
            </div>
            <div class="layui-inline">
              <label class="layui-form-label">实验日期</label>
              <div class="layui-input-inline">
                <input
                  type="text"
                  name="datetime"
                  placeholder="请填写实验日期"
                  autocomplete="off"
                  class="layui-input"
                />
              </div>
            </div>
          </div>
          <div id="Myloading">
            <img
              src="../../lib/layui-v2.5.5/css/modules/layer/default/loading-01.gif"
              alt=""
            />
          </div>

          <!-- 选项卡 -->
          <div class="layui-card">
            <!-- <div class="layui-card-header">设备检修</div> -->
            <div class="layui-card-body">
              <div class="layui-tab layui-tab-brief" lay-filter="tab-all">
                <ul
                  class="layui-tab-title"
                  id="html_title"
                  style="margin-bottom: 19px"
                ></ul>
                <div class="layui-tab-content" id="html_content"></div>
              </div>
            </div>
          </div>

          <script type="text/html" id="currentTableBar">
            <a lay-event="edit"
              ><img src="../../images/icon-qxt.png" alt=""
            /></a>
          </script>

          <div class="layui-form-item">
            <div class="layui-input-block">
              <button
                class="layui-btn"
                lay-submit
                lay-filter="saveBtn"
                style="margin-left: 90%"
              >
                确认保存
              </button>
            </div>
          </div>
        </div>
      </div>
    </div>
    <script src="../../lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
    <script src="../../js/lay-config.js?v=1.0.4" charset="utf-8"></script>
    <script>
      layui.use(["form", "element", "table", "miniTab"], function () {
        // 2020-03-25 HF 保存token
        var token = sessionStorage.getItem("newtoken");
        var examine_type = sessionStorage.getItem("HsaveExamine_type");
        var examine_code = sessionStorage.getItem("HsaveExamine_code");
        var examine_name = sessionStorage.getItem("HsaveExamine_url");

        var $ = layui.jquery,
          form = layui.form,
          ele = layui.element,
          miniTab = layui.miniTab,
          table = layui.table;
        miniTab.listen();

        var html_title = "";
        var experimentMap = null;
        var experimentMap_check = null;
        var html_content = "";

        form.on("submit(saveBtn)", function (data) {
          console.log(data, "examine_add");
          var iframeIndex = parent.layer.getFrameIndex(window.name);
          // 最后的表格数据填充
          experimentMap_check.auditors = data.field.auditors;
          experimentMap_check.datetime = data.field.datetime;
          experimentMap_check.experimentMap = experimentMap;
          experimentMap_check.experimenter = data.field.experimenter;
          experimentMap_check.report = data.field.report;

          console.log(experimentMap_check);

          layer.confirm(
            "确定增加该数据吗？",
            {
              btn: ["确定", "取消"], //按钮
            },
            function () {
              $.ajax({
                url: "/oms-web/examines/save/authority?token=" + token,
                dataType: "json",
                type: "post",
                data: JSON.stringify(experimentMap_check),
                contentType: "application/json",
                success: function (data) {
                  if (data.code == -1) {
                    window.location = "../login-1.html";
                  }
                  if (data.code === 0) {
                    layer.msg(data.msg, { icon: 1, time: 1500 }, function () {
                      parent.layer.close(iframeIndex);
                      window.parent.location.reload(); //修改成功后刷新父界面
                    });
                  } else {
                    layer.msg(data.msg, { icon: 1, time: 1000 });
                  }
                },
                error: function () {
                  layer.msg("数据请求错误！");
                },
              });
            }
          );
        });

        // 获取头表  类型
        $.ajax({
          url:
            "/oms-web/examines/" +
            examine_type +
            "/" +
            examine_code +
            "/" +
            examine_name +
            "/initialize/authority?token=" +
            token,
          dataType: "json",
          type: "get",
          contentType: "application/json",
          success: function (data) {
            if (data.code == -1) {
              window.location = "../login-1.html";
            }
            if (data.code === 0) {
              experimentMap_check = data.data;
              $("#Myloading").remove();
              var ind = 0;
              for (var key in data.data.experimentMap) {
                ind++;
                html_title += "<li>" + key + "</li>";
                html_content +=
                  '<div class="layui-tab-item"><div class="layui-tab layui-tab-card" lay-filter="tab-all2"><ul class="layui-tab-title" id="uu_' +
                  ind +
                  '"></ul><div class="layui-tab-content" id="mk_' +
                  ind +
                  '"><div class="layui-tab-item"><table class="layui-hide" id="HcurrentTableId_' +
                  ind +
                  '" lay-filter="currentTableFilter"></table></div></div></div></div>';
              }
              experimentMap = data.data.experimentMap;
              $("#html_content").html(html_content);
              $("#html_title").html(html_title);
              //   //  默认显示第一个tab
              //   $("#html_title").children(":first").addClass('layui-this');

              //   点击展开首个
              $("#html_title").children(":first").click();
              $("#html_content").children(":first").addClass("layui-show");
            } else {
              layer.msg(data.msg, { icon: 1, time: 1000 });
            }
          },
          error: function () {
            layer.msg("数据请求错误！");
          },
        });

        // 根据表头  切换表格
        ele.on("tab(tab-all)", function (data) {
          var reportName = $(this).text();
          sessionStorage.setItem("Hsavecheck_1name", $(this).text());
          var num = 0;
          for (var key in experimentMap) {
            var html_title2 = "";
            num++;
            for (var item in experimentMap[key]) {
              if (key == reportName) {
                html_title2 += "<li>" + item + "</li>";
              }
            }
            var uu_id = "#uu_" + num;
            $(uu_id).html(html_title2);
            $(uu_id).children(":first").click();
          }
        });

        ele.on("tab(tab-all2)", function (data) {
          var reportName = sessionStorage.getItem("Hsavecheck_1name");
          sessionStorage.setItem("Hsavecheck_2name", $(this).text());
          var name_two = $(this).text();
          var iu = 0;
          for (var x in experimentMap) {
            iu++;
            if (reportName == x) {
              for (var n in experimentMap[x]) {
                if (name_two == n) {
                  examine_content_2(experimentMap[x][n], iu);
                }
              }
            }
          }
        });

        // 表格
        function examine_content_2(reportName, iu) {
          var elem_id = "#HcurrentTableId_" + iu;
          var tab_id = "#mk_" + iu;
          $(tab_id).children(":first").addClass("layui-show");

          table.render({
            elem: elem_id,
            data: reportName,
            toolbar: "#toolbarDemo",
            parseData: function (res) {
              //res 即为原始返回的数据
              return {
                code: res.code, //解析接口状态
                msg: res.msg, //解析提示文本
                count: res.data.total, //解析数据长度
                data: res.data.list, //解析数据列表
              };
            },
            defaultToolbar: [
              "filter",
              "exports",
              "print",
              {
                title: "提示",
                layEvent: "LAYTABLE_TIPS",
                icon: "layui-icon-tips",
              },
            ],

            cols: [
              [
                //复选框 {type: "checkbox", width: 50, fixed: "left"},
                { type: "numbers", width: 120, title: "序号", align: "center" },
                {
                  field: "name",
                  minWidth: 110,
                  title: "数据名称",
                  align: "center",
                },
                {
                  field: "value",
                  minWidth: 110,
                  title: "数值",
                  align: "center",
                  edit: "text",
                },
                {
                  field: "unit",
                  minWidth: 110,
                  title: "单位",
                  align: "center",
                },
                {
                  field: "remarks",
                  title: "备注",
                  minWidth: 110,
                  align: "center",
                },
                {
                  title: "操作",
                  width: 150,
                  templet: "#currentTableBar",
                  fixed: "right",
                  align: "center",
                },
              ],
            ],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 15,
            page: false,
            done: function (res) {
              if (res.code == -1) {
                window.location = "../login-1.html";
              }
              // 表格渲染完成回调
            },
          });
        }

        /**
         * toolbar监听事件
         */
        table.on("toolbar(currentTableFilter)", function (obj) {});

        //监听表格复选框选择
        table.on("checkbox(currentTableFilter)", function (obj) {});

        // 获取列表data
        table.on("tool(currentTableFilter)", function (obj) {
          sessionStorage.setItem("HsaveItemName", obj.data.name);
          layer.open({
            title: "数据曲线",
            type: 2,
            shade: 0.2,
            maxmin: true,
            shadeClose: true,
            area: ["80%", "80%"],
            content: ["./examine_line.html"],
          });
        });
      });
    </script>
  </body>
</html>
